<?php
/**
 * Created by DRCOM.
 * User: GZX
 * Datetime: 2021-04-26 13:46
 */

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

class Douyin extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

//    小程序登录
    public function login(){
        $code = input('code'); // code
        $appid = input('appid');
        $secret = input('secret','17d052aa1818d12b60f6c8a97a4e4fcc');
        $wxauth = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
        $result = file_get_contents($wxauth);

        if (!$result){
            $this->error('登录失败');
        }
        $res = json_decode($result,true);
        if(!isset($res['openid'])) $this->error('获取openid失败');

        $result = array(
//            'url' =>  '/pages/release/weather',
            'url' => '/pages/douyin/video',
            'openid'   =>  $res['openid']
        );
        return json($result);
    }

    public function index()
    {
        $url = input('url','https://v.douyin.com/j3BbeLV/');
        $url = urldecode($url);

        $html = curlHelper($url);
        $url = $html['detail']['redirect_url'];
        if (preg_match('/video\/(.*?)\//', $url, $match_vid)) {
            $vid = $match_vid[1];
            return $this->success($vid);
        } else {
            return $this->error('url error');
        }
    }

    public function video()
    {
        $vid = input('vid','6853702241870793984');
        $itemUrl = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=$vid";
        $html = file_get_contents($itemUrl);
        $videoObj = json_decode($html, true);
        if ($videoObj['status_code'] == 0) {
            $videoUri = $videoObj['item_list'][0]['video']['play_addr']['url_list'][0];
            $result['desc'] = $videoObj['item_list'][0]['desc'];
            if (trim($result['desc']) == '') $result['desc'] = '无标题';
            $result['author'] = $videoObj['item_list'][0]['author']['nickname'];
            $result['cover'] = $videoObj['item_list'][0]['video']['cover']['url_list'][0];
            if ($videoUri) {
                $url = str_replace('playwm', 'play', $videoUri);
                $html = curlHelper($url, null, ['user-agent:Aweme/26006 CFNetwork/902.2 Darwin/17.7.0']);
                $result['video'] = $html['detail']['redirect_url'];
                $result['vid'] = $vid;
            }
        }
        return $this->success($result);
    }

    public function downVideoUrl()
    {
        $vid = input('vid',7137641282482048287);
        $video = "/tmp/vid/$vid.mp4";
        if (file_exists($video)){
            $this->success($video);
        }
        $itemInfo = $this->getItemInfo($vid);

        $mp4 = $itemInfo['video'];
        $content = @file_get_contents(input('url'));
        $file = '/tmp/111.mp4';
        file_put_contents($file, $content);
        $size = filesize($file);
        header("Content-type: video/mp4");
        header("Accept-Ranges: bytes");
        if (isset($_SERVER['HTTP_RANGE'])) {
            header("HTTP/1.1 206 Partial Content");
            list($name, $range) = explode("=", $_SERVER['HTTP_RANGE']);
            list($begin, $end) = explode("-", $range);
            if ($end == 0) {
                $end = $size - 1;
            }
        } else {
            $begin = 0;
            $end = $size - 1;
        }
        header("Content-Length: " . ($end - $begin + 1));
        header("Content-Disposition: filename=" . basename($file));
        header("Content-Range: bytes " . $begin . "-" . $end . "/" . $size);
        $fp = fopen($file, 'rb');
        fseek($fp, $begin);
        while (!feof($fp)) {
            $p = min(1024, $end - $begin + 1);
            $begin += $p;
            echo fread($fp, $p);
        }
        fclose($fp);
    }

    public function downvideo(){
        $vid = input('vid',7137641282482048287);
        $video = "/tmp/vid/$vid.mp4";
        if (file_exists($video)){
            $this->success(substr($video,4));
        }
        $itemInfo = $this->getItemInfo($vid);

        $mp4 = $itemInfo['video'];
        $cmd = shell_exec("wget \"$mp4\" -O /tmp/vid/$vid.mp4");
        $this->success(substr($video,4));

    }

}

?>

